﻿Imports System.IO

Public Class FormEmployee
    Dim myEmployee As DataTable
    Dim dView As DataView
    Dim i As Int16
    Private Sub LabelX1_Click(sender As Object, e As EventArgs)
        Me.Close()

    End Sub

    Private Sub lblMaximize_Click(sender As Object, e As EventArgs)
        If Me.WindowState = FormWindowState.Normal = False Then

            Me.WindowState = FormWindowState.Normal
        Else

            Me.WindowState = FormWindowState.Maximized



        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs)
        'Format time
        lblShowTime.Text = Now.ToLongTimeString
    End Sub

    Private Sub FormEmployee_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
        '====================
        Me.btnAdd.Enabled = App.IsAuthorized(Permissions.Manage_Employee_Add)
        Me.btnUpdate.Enabled = App.IsAuthorized(Permissions.Manage_Employee_Edit)
        Me.btnDelete.Enabled = App.IsAuthorized(Permissions.Manage_Employee_Delete)
        Me.btnDeatil.Enabled = App.IsAuthorized(Permissions.Manage_Employee_Detail)
        '==================
        lblShowTime.ForeColor = Color.White
        lblShowDate.ForeColor = Color.White
        lblShowDate.Text = (Now.ToLongDateString)

        UI.FormatDataGrid(dgvEmployee)
    End Sub

    Private Sub lblMaximize_Click_1(sender As Object, e As EventArgs)
        If Me.WindowState = FormWindowState.Normal = False Then
            Me.WindowState = FormWindowState.Normal
        Else
            Me.WindowState = FormWindowState.Maximized
        End If
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()

        initalizeData()
    End Sub
    Private Sub initalizeData()
        Try
            loadData()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub loadData()
        'Get Data From Database
        Try
            myEmployee = Employees.GetAllEmployees
            dgvEmployee.DataSource = myEmployee
        Catch ex As Exception

        End Try
    End Sub
 

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Get last DataGridView Rows when click button add
        i = dgvEmployee.Rows.Count.ToString
        i = i - 1
        dgvEmployee.CurrentCell = dgvEmployee.Rows(i).Cells(1)
        Dim frm As New FormEmployeeAED

        frm.ShowDialog()

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub
            
            End If

        Catch ex As Exception

        End Try
        Try
            FormDelete.ShowDialog()
            If FormDelete.DialogResult = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If


            'Remove DataGridView Row and Delete data in Database
            For Each row As DataGridViewRow In dgvEmployee.SelectedRows
                If row.Cells("EMPLOYEE_ID").Value = Nothing Then
                    Exit Sub
                End If
                Dim emp As New EmployeeInfo
                emp.EmployeeId = row.Cells("EMPLOYEE_ID").Value

                Employees.DeleteEmployee(emp)

                Me.dgvEmployee.Rows.Remove(row)
                dgvEmployee.CurrentCell = dgvEmployee.Rows(0).Cells(1)


            Next


        Catch ex As Exception

        End Try


    End Sub
    Public Sub detailUpdate()
        'pass values from DataGridView to FormEmployeeAED when click Button Detail
        Try


            Dim row = Me.dgvEmployee.CurrentRow

            With FormEmployeeAED

                If row.Cells("EMPLOYEE_ID").Value Is Nothing Then
                    Exit Sub
                Else
                    .empId = row.Cells("EMPLOYEE_ID").Value
                    .txtEmpCode.Text = row.Cells("EMPLOYEE_CODE").Value
                    .txtEmpEngName.Text = row.Cells("ENGLISH_NAME").Value
                    .txtEmpKhName.Text = row.Cells("KHMER_NAME").Value
                    .cboSex.Text = row.Cells("SEX").Value
                    .txtDOB.Text = row.Cells("DOB").Value
                    .txtAddress.Text = row.Cells("ADDRESS").Value
                    .cboStatus.Text = row.Cells("MARITAL_STATUS").Value
                    .txtIdCard.Text = row.Cells("ID_CARD").Value
                    .txtPhone.Text = row.Cells("PHONE").Value
                    .txtEmail.Text = row.Cells("EMAIL").Value
                    .txtStartDate.Text = row.Cells("START_DATE").Value
                    .txtContract.Text = row.Cells("TYPE_OF_CONTRACT").Value
                    .txtSalaryStart.Text = row.Cells("SALARY_START_DATE").Value
                    .cboBlood.Text = row.Cells("BLOOD").Value
                    .cboUser.Text = row.Cells("MODIFY_BY").Value
                    .cboProvince.Text = row.Cells("PROVINCE_NAME").Value
                    .cboGroup.Text = row.Cells("DEPARTMENT").Value
                    .cboPosition.Text = row.Cells("POSITION").Value
                    .txtSalary.Text = row.Cells("SALARY").Value
                    .txtNote.Text = row.Cells("NOTE").Value

                    Dim img As Byte() = DirectCast(row.Cells("Photo").Value, Byte())
                    Dim ms As New MemoryStream(img)
                    .pic.Image = Image.FromStream(ms)
                    
                    .ShowDialog()
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        With FormEmployeeAED

            .btnSaveUpdate.Show()
            .btnClose.Show()
        End With
        For Each cbo As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf cbo Is ComboBox Then
                cbo.Enabled = True
            End If

        Next
        For Each myText As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf myText Is TextBox Then
                myText.Enabled = True
            End If

        Next
        detailUpdate()
    End Sub

    Private Sub btnDeatil_Click(sender As Object, e As EventArgs) Handles btnDeatil.Click
        With FormEmployeeAED
           
            .btnSaveUpdate.Hide()
            .btnClose.Hide()
        End With
        For Each cbo As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf cbo Is ComboBox Then
                cbo.Enabled = False
            End If

        Next
        For Each myText As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf myText Is TextBox Then
                myText.Enabled = False
            End If

        Next
        detailUpdate()
        
    End Sub

    
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        searchDataGridView()
      
    End Sub
    Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
        '48 to 57 => 0 to 9
        '65 to 90 => A to Z
        '97 to 122 => a to z
        '8=> Backspace

        'Select Case Asc(e.KeyChar)
        '    Case 48 To 57, 65 To 90, 8


        '    Case Else
        '        '          
        '        e.Handled = True
        'End Select
         
    End Sub
    Public Sub searchDataGridView()
        'Allow user to search data in DataGridView by Name or Phone
        'CmdText.Parameters("@S") = "%" & txtSearch.Text & "%"
        Try
            Dim strFilter As String = "ENGLISH_NAME   Like  '%" & txtSearch.Text & "%'  Or KHMER_NAME Like  '%" & txtSearch.Text & "%' And ACTIVE = 1"

            dView = New DataView(myEmployee, strFilter, "", DataViewRowState.CurrentRows)

            dgvEmployee.DataSource = dView


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

​
    Private Sub dgvEmployee_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployee.CellContentClick

    End Sub
    Public rows As Integer
    Private Sub dgvEmployee_DoubleClick(sender As Object, e As EventArgs) Handles dgvEmployee.DoubleClick
        For Each row As DataGridViewRow In dgvEmployee.SelectedRows
            rows = row.Cells(0).Value

            FormReport1.ShowDialog()

        Next

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        With FormEmployeeAED

            .btnSaveUpdate.Hide()
            .btnClose.Hide()
        End With
        For Each cbo As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf cbo Is ComboBox Then
                cbo.Enabled = False
            End If

        Next
        For Each myText As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf myText Is TextBox Then
                myText.Enabled = False
            End If

        Next
        detailUpdate()
    End Sub

   

    Private Sub btnDetail_Click(sender As Object, e As EventArgs) Handles btnDetail.Click
        With FormEmployeeAED

            .btnSaveUpdate.Hide()
            .btnClose.Hide()
            .Button1.Hide()
        End With
        For Each cbo As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf cbo Is ComboBox Then
                cbo.Enabled = False
            End If

        Next
        For Each myText As Control In FormEmployeeAED.EmployeeGB.Controls
            If TypeOf myText Is TextBox Then
                myText.Enabled = False
            End If

        Next
        detailUpdate()
    End Sub
End Class